package me.qi.kancha.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.qi.kancha.dto.core.BranchDTO;
import me.qi.kancha.mapper.BranchMapper;
import me.qi.kancha.service.BranchService;
import me.qi.kancha.service.base.support.PageData;
import me.qi.kancha.vo.branch.BranchInsertVo;
import me.qi.kancha.vo.branch.BranchQueryVo;
import me.qi.kancha.vo.branch.BranchUpdateVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
* 分支机构模块-service实现
*
* @author qi
* @date 2022-10-27 04:07:06
*/
@Service
public class BranchServiceImpl extends ServiceImpl<BranchMapper, BranchDTO>
    implements BranchService{

    @Override
    public PageData<BranchDTO> listPaging(Object obj) {
        BranchQueryVo param = (BranchQueryVo) obj;
        LambdaQueryWrapper<BranchDTO> wrapper = new LambdaQueryWrapper<>();

        //动态查询条件
        if (!ObjectUtils.isEmpty(param.getKey())) {
            wrapper.like(BranchDTO::getName, param.getKey());
        }
        if (!ObjectUtils.isEmpty(param.getType())) {
            wrapper.eq(BranchDTO::getType, param.getType());
        }
        if (!ObjectUtils.isEmpty(param.getTypeNo())) {
            wrapper.ne(BranchDTO::getType, param.getTypeNo());
        }

        //默认排序方式为 created_time desc 前段未传 order 参数时生效
        Page page = page(param.pageInstance(), wrapper);

        return new PageData<>(page);
    }

    @Override
    public BranchDTO insert(Object obj) {
        BranchInsertVo param = (BranchInsertVo) obj;
        BranchDTO dto = new BranchDTO();
        BeanUtils.copyProperties(param, dto);
        save(dto);
        return dto;
    }

    @Override
    public BranchDTO update(Object obj) {
        BranchUpdateVo param = (BranchUpdateVo) obj;
        BranchDTO dto = new BranchDTO();
        BeanUtils.copyProperties(param, dto);
        updateById(dto);
        return getById(dto.getId());
    }
}




